﻿using BusinessLogic;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Logic;
using Common;
using System.IO;

namespace AppAdmin.Controllers
{
    public class ShopController : Controller
    {
        //
        // GET: /Shop/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult SetMap()
        {
            return View();
        }

        public ActionResult Form()
        {
            string keyValue = Request.QueryString["keyValue"];
            if (!string.IsNullOrEmpty(keyValue))
            {
                PicLogic pic_bll = new PicLogic();
                var result_data = pic_bll.GetPicListByTypeAndSource(Convert.ToInt32(keyValue), 1);
                ViewData["Piclist"] = result_data;
            }
            return View();
        }

        public ActionResult Details()
        {
            string keyValue = Request.QueryString["keyValue"];
            if (!string.IsNullOrEmpty(keyValue))
            {
                PicLogic pic_bll = new PicLogic();
                var result_data = pic_bll.GetPicListByTypeAndSource(Convert.ToInt32(keyValue), 1);
                ViewData["Piclist"] = result_data;
            }
            return View();
        }


        [HttpGet]
        public ActionResult GetGridJson(Pagination pagination, string keyword,string pri,string City,string Qu)
        {
            var expression = ExtLinq.True<Shop>();
            expression = expression.And(t => t.Isdelete==0);
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.ShopName.Contains(keyword.Trim()));
            }
            if (!string.IsNullOrEmpty(pri))
            {
                expression = expression.And(t => t.Province == pri);
            }
            if (!string.IsNullOrEmpty(City))
            {
                expression = expression.And(t => t.City == City);
            }
            if (!string.IsNullOrEmpty(Qu))
            {
                expression = expression.And(t => t.AreaQu == Qu);
            }
            int Total = 0;
            var list = new ShopLogic().getPageDate<Shop, int>(c => new { c.ID, c.ShopName, c.Pic, c.OpenStartTime, c.OpenEndTime, c.SetUpTime, c.Lng, c.Lat, c.IsShow, c.Isdelete, c.AddTime, c.AddPerson, c.ModifyDate, c.ModifyPerson, c.Address, c.AreaQu, c.IsOpen, c.Province, c.City, c.ShopGuid }, expression, c => c.ID, pagination.page, pagination.rows, out Total);
            pagination.records = Total;
            var data = new
            {
                rows = list,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        [HttpGet]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = new ShopLogic().GetForm(Convert.ToInt32(keyValue));
            return Content(data.ToJson());
        }
        [HttpPost]
        public ActionResult SubmitForm(FormCollection collection, string keyValue)
        {
            Shop model = new Shop();
            ShopLogic bll = new ShopLogic();
            model.ShopName = collection["ShopName"].Trim("&.nbsp;".ToCharArray());
            //model.Pic = collection["Pic"].Trim("&.nbsp;".ToCharArray());
            model.OpenStartTime =  Convert.ToDateTime(collection["OpenStartTime"].Trim());
            model.OpenEndTime =  Convert.ToDateTime(collection["OpenEndTime"].Trim());
            if (!string.IsNullOrEmpty(collection["SetUpTime"].Trim()))
            {
                model.SetUpTime = Convert.ToDateTime(collection["SetUpTime"].Trim());
            }

            model.Lng = Convert.ToDouble(collection["Lng"].Trim());
            model.Lat = Convert.ToDouble(collection["Lat"].Trim());
            model.IsShow = Convert.ToByte(collection["HD_IsShow"].Trim());
            model.Isdelete =0;
            model.Address = collection["Address"].Trim("&.nbsp;".ToCharArray());
            model.AreaQu = collection["AreaQu"].Trim();
            model.Province = collection["Province"].Trim();
            model.City = collection["City"].Trim();
            model.IsOpen = Convert.ToByte(collection["HD_IsOpen"].Trim());
            model.Pic = collection["Pic"].Trim("&.nbsp;".ToCharArray());
            model.Details = collection["Details"].Trim("&.nbsp;".ToCharArray());
            model.Tel = collection["Tel"].Trim("&.nbsp;".ToCharArray());
            

            string urls = collection["urls"].Trim("&.nbsp;".ToCharArray());
            if (!string.IsNullOrEmpty(keyValue))//修改
            {
                model.ModifyDate = DateTime.Now;
                model.ModifyPerson = ThisAdminSession.LoginName;
                model.ID = Convert.ToInt32(keyValue);
                var tem = bll.UpdateInfo(model);
                if (tem > 0 &&!string.IsNullOrEmpty(urls))//修改成功
                {
                    bll.Pic_Shop(urls, model.ID);
                }
            }
            else
            {
                model.ModifyDate = DateTime.Now;
                model.ModifyPerson = ThisAdminSession.LoginName;
                model.AddTime = DateTime.Now;
                model.AddPerson = ThisAdminSession.LoginName;
                model.ShopGuid = Guid.NewGuid().ToString();
                var tem = bll.AddInfo(model);
                if (tem > 0 && !string.IsNullOrEmpty(urls))
                {
                    bll.Pic_Shop(urls, tem);
                }
            }


            return Content(new AjaxResult { state = ResultType.success.ToString(), message = "操作成功。" }.ToJson());
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            ShopLogic bll = new ShopLogic();
            bll.DelInfo(Convert.ToInt32(keyValue));
            return Content(new AjaxResult { state = ResultType.success.ToString(), message = "删除成功。" }.ToJson());
        }
        public ActionResult Open(string keyValue)
        {
            ShopLogic bll = new ShopLogic();
            bll.Open(Convert.ToInt32(keyValue));
            return Content(new AjaxResult { state = ResultType.success.ToString(), message = "操作成功。" }.ToJson());
        }
        public ActionResult Close(string keyValue)
        {
            ShopLogic bll = new ShopLogic();
            bll.UnOpen(Convert.ToInt32(keyValue));
            return Content(new AjaxResult { state = ResultType.success.ToString(), message = "操作成功。" }.ToJson());
        }
        public ActionResult Show(string keyValue)
        {
            ShopLogic bll = new ShopLogic();
            bll.Show(Convert.ToInt32(keyValue));
            return Content(new AjaxResult { state = ResultType.success.ToString(), message = "操作成功。" }.ToJson());
        }
        public ActionResult notShow(string keyValue)
        {
            ShopLogic bll = new ShopLogic();
            bll.UnShow(Convert.ToInt32(keyValue));
            return Content(new AjaxResult { state = ResultType.success.ToString(), message = "操作成功。" }.ToJson());
        }
        [HttpGet]
        public ActionResult UploadPic()
        {
            return View();
        }
    }
}
